//
//  ViewController.swift
//  TransitionView
//
//  Created by flipped on 2025/1/15.
//

import UIKit

class ViewController: UIViewController {
  @IBOutlet var img2: UIImageView!
  @IBOutlet var img1: UIImageView!

  private var retVisible: Bool = true
  override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(img1)
    img1.frame = view.bounds

    //		指定初始值，防止位置不一，显示问题
    img2.frame = view.bounds
  }

  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    //		闭包的两种声明方式
//    let comp = { (finish: Bool) in
//      if finish { self.retVisible.toggle() }
//    }
    let comp1: (Bool) -> Void = { finish in
      if finish == true { self.retVisible.toggle() }
    }
    if retVisible {
      UIView.transition(from: img1, to: img2, duration: 1, options: UIView.AnimationOptions.transitionFlipFromLeft) { finish in
        if finish {
          self.retVisible.toggle()
        }
      }
    } else {
      UIView.transition(from: img2, to: img1, duration: 1, options: UIView.AnimationOptions.transitionFlipFromRight, completion: comp1)
    }
  }
}
